//
//  SRPacket.h
//  sr01sdkProject
//
//  Created by Linktop on 2022/5/7.
//  打包命令

#import <Foundation/Foundation.h>
#import "SRBLeService.h"
#import "SRDeviceInfo.h"
#import "SrEcgData.h"
#import "SrEcgAlgorithmResults.h"
NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(UInt8, PACK_CMD) {
    PACK_CMD_SET_SPORT_MODE = 0x01,         //运动模式设置
    PACK_CMD_SET_HR_TEMP_MEASURE = 0x02,    // 体温 心率设置
    PACK_CMD_SET_OXYGEN_MEASURE = 0x03,     //血氧测量设置
    PACK_CMD_SYNC_TIME = 0x04,              // 时间同步
    PACK_CMD_GET_OXYGEN_HRV = 0x05,         // 获取心率血氧
    PACK_CMD_GET_STEPS = 0x06,              //获取计步
    PACK_CMD_GET_TEMPERATURE = 0x07,        //获取体温
    PACK_CMD_SHUT_DOWN = 0X08,              // 关机
    PACK_CMD_REBOOT = 0X09,                 // 重启
  
    PACK_CMD_FACTORY_RESET = 0x0a,          // 恢复出厂设置
    
    PACK_CMD_HIS_COUNT = 0x0c,              // 历史数据个数
    PACK_CMD_HIS_DATA = 0x0d,               // 历史数据
    PACK_CMD_CLEAR_HIS_DATA = 0x0e,         // 清空历史
    PACK_CMD_DEVICE_INFO = 0x0f,            // 设备信息
    PACK_CMD_SN = 0x10,                     // sn
    PACK_CMD_BATTERY = 0X11,                // 电池电量
    PACK_CMD_SOS_SETTING = 0X13,            // sos setting
    PACK_CMD_BIND = 0X15,                   // 绑定解绑设备
    PACK_CMD_SETTING_SPO_HR_REALTIMEMEASURE = 0X16,                   // 实时测量心率血氧参数设置

    PACK_CMD_START_MANUFACT_CER = 0x1B,     // 开始厂家验证
    PACK_CMD_MANUFACT_SEND_R2 = 0x1C,       // 向设备发送解密原文
    PACK_CMD_SPORT_MODE = 0x20,             // 运动模式
    PACK_CMD_FUNCTIN_SWITCH = 0x31,         // 设备功能开关
    PACK_CMD_FUNCTIN_SET_HR_MEASURE_DURATION = 0x3C, // 设置测量时长
    PACK_CMD_FUNCTIN_GET_DEVICEINFO_5 = 0x3d, // 获取设备信息5 心率测量时长 回包0x8f
    PACK_CMD_FUNCTIN_SET_STEP_THRESHOLD = 0x3F, // 设置计步阈值
    PACK_CMD_FUNCTIN_ECG = 0x52,             // 仅sr23可用 回包 0x94
    PACK_CMD_FUNCTIN_ECG_PPG = 0x53,         // 仅sr23可用  回包 0x85
};// 下发命令字

typedef NS_ENUM(UInt8, DEVICE_RESPONSE) {
    DEVICE_RESPONSE_RESPCMD =           0x80, // 收包确认
    DEVICE_RESPONSE_HISTORY_COUNT =     0X81, // 历史数据个数
    DEVICE_RESPONSE_HISTORY_DATA =      0X82, // 历史数据上报
    DEVICE_RESPONSE_OXYGEN_HRV =        0x83, // 上报心率血氧
    DEVICE_RESPONSE_STEP_DATA =         0X84, //计步数据
    DEVICE_RESPONSE_TEMPRETRUE_DATA =   0X85, // 体表温度
    DEVICE_RESPONSE_BATTERY_LEVEL =     0X86, //电池电量和充电
    DEVICE_RESPONSE_DEVICEINFO_DATA =   0X87, //设备信息上报
    DEVICE_RESPONSE_SN_DATA =           0X88, //设备信息SN上报
    DEVICE_RESPONSE_SOS =               0X89, //sos触发上报
    DEVICE_RESPONSE_SOS_MEASUREDATA =   0X8A, //sos测量数据
    DEVICE_RESPONSE_MANUFAT_R1 =        0x8D, //上报厂家验证 R1
    DEVICE_RESPONSE_MANUFAT_RES =       0x8E, // 上报厂家验证结果
    DEVICE_RESPONSE_HR_MEASURE_DURATION =       0x8F, // 设备信息5 上报设备心率测量时长
    DEVICE_RESPONSE_HISTORY_DATA_2 =      0X91, // 历史数据上报, 包含运动模式
    DEVICE_RESPONSE_HISTORY_DATA_3 =      0X92, // 历史数据上报, 计步数据不用修正
    
    DEVICE_RESPONSE_ECG_ROW =           0X94, // ecg上报 原始数据
    DEVICE_RESPONSE_ECG_PPG_ROW =       0X95, // ecg+ppg上报 原始数据
    DEVICE_RESPONSE_ECG_PPG_FILTER =    0X96, //ecg 算法滤波后波形数据
    DEVICE_RESPONSE_ECG_RESULT =        0X97, //ecg算法计算结果
    DEVICE_RESPONSE_ECG_LEAD_ON =       0X98, // ecg手指接触状态

    DEVICE_RESPONSE_Infrared_Data_Sources = 0XBB, //红外源数据上报
    DEVICE_RESPONSE_Red_light_source_data = 0XBC,// 红光源数据上报
    DEVICE_RESPONSE_IR_AND_RED_SOURCE = 0xBD,// 红外红光合并上报 SR09N_V2.1.5和SR09W_V1.3.2
}; // 设备上报协议

typedef NS_ENUM(uint8_t, SDKSportType) { // 下发运动类型
    SDK_SP_OTHER = 0,  // 除了跑步以外的
    SDK_SP_RUNNING = 1, // 跑步
};

@interface SRPacket : NSObject

@property(weak, nonatomic)id<StLeKeyFobProtocol> dataDelegate;

@property(assign, nonatomic)BOOL isTranslatingHistory; //标记是否传输历史数据

@property(strong, nonatomic)NSTimer *hisTimer;
@property(assign, nonatomic, readonly)uint8_t projectId;


/// 获取心率血氧数据
/// @param isOpen 开关
+(NSData *)packGetOxygenHrvBoth:(BOOL)both IsOpen:(BOOL)isOpen;

-(void)receiveData:(NSData *)data;
/// 重启
+(NSData *)packReboot;
/// 关机
+(NSData *)packShutDown;
/// 获取电量和充电状态
+(NSData *)packBattery;

+(NSData *)packSN;
// 历史个数
+(NSData *)packHistoryCount;
// 历史数据
+(NSData *)packHistoryData;
// 清空设备历史
+(NSData *)packClearHistory;

+(NSData *)packDevInfo;

//实时测量参数设置
+(NSData *)packSettingReralMeasure:(uint8_t)sampleRate IswaveOn:(BOOL)isWaveOn;

+(NSData *)packTimeSync; //时间同步
+(NSData *)packBindDevice:(BOOL)bind;

+(NSData *)packGetSteps;
+(NSData *)packGetTemperature;
+(NSData *)PackReset;

// 下发获取设备信息5
+(NSData *)PackGetDevInfoFive;

// 下发设置测量时长 单位 秒 8-180s
+(NSData *)PackSetHrMeasureDuration:(NSInteger)seconds;

/// 运动模式
/// @pram start YES = 开始 NO = 停止
/// @pram valInterval 设备取值间隔 单位秒
/// @pram durationMIn 持续时长
+(NSData *)packSoprtMode:(BOOL)start
           ValueINterval:(uint16_t)valInterval
                Duration:(uint16_t)durationMIn
               SportType:(SDKSportType)spType;

/// 开始厂家验证
+(NSData *)packStartAes;

/// 下发解密原文
+(NSData *)packSendR2:(NSData *)r2;

// 血氧测量设置
+(NSData *)packSettingOxygenMeasure:(NSUInteger)startHor StartMin:(NSUInteger)startMin
                           StartSec:(NSUInteger)startSecond Endhor:(NSUInteger)endHor
                             EndMin:(NSUInteger)endMin EndSec:(NSUInteger)endSec
                                Interval:(NSUInteger)interval IsOn:(BOOL)isOn;
// 心率 体温设置
+(NSData *)packSettingHeartRateMeasurInterval:(NSUInteger)hrInterval SkinTemperatureInterval:(NSUInteger)skinTemperatureInterval IsOn:(BOOL)isOn;

+(NSData *)packSportMode:(NSUInteger)startHor StartMin:(NSUInteger)startMin
                StartSec:(NSUInteger)startSecond Endhor:(NSUInteger)endHor
                  EndMin:(NSUInteger)endMin EndSec:(NSUInteger)endSec
              HrIntreval:(NSUInteger)hrInterval SkinTemperatureInterval:(NSUInteger)skinTemperatureInterval
                BloodOxygenInterval:(NSUInteger)oxygeninterval
                                IsOn:(BOOL)isOn;

+(NSData *)packSOSSettingDoubleTapTimes:(uint8_t)doubleTapTimes
                                       SingleTapInterval:(uint8_t)singleTapInterval TapDetectionThreshold:(uint8_t)tapDetectionThreshold
           DoubleTapEffectStartTime:(uint8_t)doubleTapEffectStartTime DoubleTapEffectEndTime:(uint8_t)doubleTapEffectEndTime Open:(BOOL)sosOpen;

+(NSData *)PackEcgMeasure:(BOOL)on SampleRate:(uint16_t)sampleRate InnerClockSource:(uint8_t)clockSource EcgPgaGain:(uint8_t)ecgPgaGain DispSrc:(uint8_t)dispSrc;
+(NSData *)PackEcgAndPpgMeasure:(BOOL)on;

+(uint8_t)crc8:(NSData *)packData;
+(BOOL)crc8Checker:(NSData *) packData;

@end

NS_ASSUME_NONNULL_END
